﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Entity.vo;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentUpgradeMaintainExtra : BasePage
    {
        public Guid MaintainID
        {
            get
            {
                if (hiddenMaintainID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenMaintainID.Value);
            }
            set
            {
                hiddenMaintainID.Value = value.ToString();
            }
        }

        public Guid MaintainMan
        {
            get
            {
                if (hiddenMaintainMan.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenMaintainMan.Value);
            }
            set
            {
                hiddenMaintainMan.Value = value.ToString();
            }
        }

        public String MaintainManName
        {
            get
            {
                if (hiddenMaintainManName.Value == string.Empty) return String.Empty;
                else return hiddenMaintainManName.Value;
            }
            set
            {
                hiddenMaintainManName.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (Request.QueryString["MaintainID"] != null) { 
                    MaintainID = new Guid(Request.QueryString["MaintainID"] );
                    WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
                    MaintainMan = maintainEntity.Maintainer;
                    if(MaintainMan !=Guid.Empty)
                        MaintainManName = new SystemBll().Get<SysUserEntity>(MaintainMan).UserName;
                    WsmTaskRegisterEntity registerEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
                    List<WsmAccessoryUpgradeRegisterEntity> upgradeList = new SystemBll().GetAll<WsmAccessoryUpgradeRegisterEntity>(" and RegisterID = '" + registerEntity.RegisterID.ToString() + "'");
                    String AccessoryIds = "";
                    foreach (WsmAccessoryUpgradeRegisterEntity upgradeEntity in upgradeList) {
                        if (upgradeEntity.SoftwareVersion != null) {
                            SoftwareVersion.Text = upgradeEntity.SoftwareVersion;
                        }
                        if (AccessoryIds != "") {
                            AccessoryIds += ",";
                        }
                        AccessoryIds = AccessoryIds + "'" + upgradeEntity.AccessoryID.ToString() + "'";
                    }
                    if (AccessoryIds != "") {
                        DataTable dt = new CommonBll().getDataFromSQL(" select AccessoryID,CompanyInstrumentCode,AccessoryName,Unit,coStockquantity  from vw_StockQuery where AccessoryId in ("+AccessoryIds+") group by AccessoryID,CompanyInstrumentCode,AccessoryName,Unit,coStockquantity");                
                        gridSubItems.DataSource = dt;
                        gridSubItems.DataBind();
                    }
                }
            }
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < gridSubItems.Items.Count; index++) {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label coStockquantity = gridSubItems.Items[index].FindControl("coStockquantity") as Label;
                Label personalStockquantity = gridSubItems.Items[index].FindControl("personalStockquantity") as Label;
                RadNumericTextBox usequantity = gridSubItems.Items[index].FindControl("usequantity") as RadNumericTextBox;
                new SystemBll().Delete<WsmAccessoryReplacementEntity>(" and MaintainID='" + MaintainID.ToString() + "'");
                if (usequantity.Text.Trim() != "") {
                    WsmAccessoryReplacementEntity entity = new WsmAccessoryReplacementEntity();
                    entity.ReplacementID = Guid.NewGuid();
                    entity.MaintainID = MaintainID;
                    entity.AccessoryID = new Guid(AccessoryID.Text);
                    entity.ReplacementNum = System.Convert.ToInt32(usequantity.Value);
                    entity.Deleted = false;
                    new SystemBll().Insert(entity);
                }
            }
        }

        protected void gridSubItems_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
            {
                Label AccessoryID = gridSubItems.Items[e.Item.ItemIndex].FindControl("AccessoryID") as Label;
                Label performer = gridSubItems.Items[e.Item.ItemIndex].FindControl("performer") as Label;
                Label personalStockquantity = gridSubItems.Items[e.Item.ItemIndex].FindControl("personalStockquantity") as Label;
                RadNumericTextBox usequantity = gridSubItems.Items[e.Item.ItemIndex].FindControl("usequantity") as RadNumericTextBox;
                if (AccessoryID != null)
                {
                    performer.Text = MaintainManName;
                    DataTable dt = new CommonBll().getDataFromSQL(" select  *  from vw_StockQuery where AccessoryId = '" + AccessoryID.Text + "'  and Receiver='" + MaintainMan.ToString() + "'");
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        personalStockquantity.Text = dt.Rows[0]["PersonalStockquantity"].ToString();
                    }
                    else
                    {
                        personalStockquantity.Text = "0";
                    }
                    List<WsmAccessoryReplacementEntity> list = new SystemBll().GetAll<WsmAccessoryReplacementEntity>(" and MaintainID='" + MaintainID.ToString() + "' and AccessoryID='" + AccessoryID.Text + "'");
                    if (list != null && list.Count > 0)
                    {
                        usequantity.Value = System.Convert.ToDouble(list[0].ReplacementNum);
                    }
                }
            }
        }
    }
}
